﻿using EFAPIProject.Entities;
using static EFAPIProject.Repositories.IRepository;

namespace EFAPIProject.Services
{
    public class OrderService
    {
        private readonly IRepository<Order> _orderRepository;

        public OrderService(IRepository<Order> orderRepository)
        {
            _orderRepository = orderRepository;
        }

        // 创建订单
        public async Task CreateOrderAsync(Order order)
        {
            // 生成唯一订单号（示例逻辑）
            order.OrderNo = $"ORD{DateTime.Now:yyyyMMddHHmmss}";
            order.CreateTime = DateTime.Now;

            await _orderRepository.AddAsync(order);
            await _orderRepository.SaveChangesAsync();
        }

        // 查询用户的所有订单
        public async Task<List<Order>> GetUserOrdersAsync(int userId)
        {
            return await _orderRepository.GetFilteredAsync(o => o.UserId == userId);
        }
    }
}
